<template>
  <!-- eslint-disable vue/require-component-is -->
  <component v-bind="linkProps(to)">
    <slot />
  </component>
</template>

<script>
import { isExternal } from '@/utils/validate'

export default {
  props: {
    to: {
      type: String,
      required: true
    },
    query: {
      type: Object,
      required: false,
      default: function() {
        return {}
      }
    },
    target: {
      type: String,
      default: 'default'
    }
  },
  methods: {
    linkProps(url) {
      if (this.target === 'blank') {
        if (!isExternal(url)) {
          url = this.$store.state.app.server_api + url
        }
        return {
          is: 'a',
          href: url,
          target: '_blank',
          rel: 'noopener'
        }
      }

      if (url.startsWith('/views/iframe')) {
        return {
          is: 'router-link',
          to: {
            path: '/views/iframe',
            query: this.query
          }
        }
      }

      return {
        is: 'router-link',
        to: {
          path: url,
          query: this.query
        }
      }
    }
  }
}
</script>
